package com.iotek.ssm.interceptor;

import com.iotek.ssm.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.handler.Handler;

public class UserLoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 得到请求的url
        String url = request.getRequestURL().toString();
        System.out.println("*************URL = " + url + "**********UserLoginInterceptor");
        // 判断是否公开
//        if (url.contains("login") || url.contains("images") || url.contains("css") || url.contains("script") || url.contains("register"))
//            return true;
//        if (url.contains("register"))
//            return true;
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        //判断用户信息是否存在
        if (user != null) {
            return true;
        }
        //跳转登录页面
        request.getRequestDispatcher("../user/login.jsp").forward(request, response);
        return false;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
